package com.yc.SelectCourse.ui;

import org.eclipse.swt.widgets.Composite;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Label;

import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.SelectCourse.biz.BizException;
import com.yc.SelectCourse.biz.SelectCourseBiz;
import com.yc.SelectCourse.dao.SelectCourseDao;
import com.yc.SelectCourse.util.StuUtils;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class StudentSelectCourseCmp extends Composite {
	private Table table;
	private Text text_nrong;
	private Text text_id;
	private Text text_name;
	private Text text_credite;
	private Text text_teacher;
	private Combo combo_type;
	private TableColumn numTbl;
	private SelectCourseDao dao = new SelectCourseDao();
	private int page=1;
	private int size=15;
	private int endPage;
	private List<Map<String,Object>> date=null;
	private Map<String,Object> courseNum = new LinkedHashMap<String, Object>();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public StudentSelectCourseCmp(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		setLayout(new FormLayout());
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0, 50);
		fd_table.left = new FormAttachment(0, 10);
		fd_table.right = new FormAttachment(100, -10);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn idTbl = new TableColumn(table, SWT.NONE);
		idTbl.setWidth(137);
		idTbl.setText("课程编号");
		
		TableColumn nameTbl = new TableColumn(table, SWT.NONE);
		nameTbl.setWidth(117);
		nameTbl.setText("课程名称");
		
		TableColumn creditTbl = new TableColumn(table, SWT.NONE);
		creditTbl.setWidth(109);
		creditTbl.setText("学分");
		
		TableColumn teacherTbl = new TableColumn(table, SWT.NONE);
		teacherTbl.setWidth(100);
		teacherTbl.setText("任课教师");
		
		TableColumn roomTbl = new TableColumn(table, SWT.NONE);
		roomTbl.setWidth(100);
		roomTbl.setText("教室");
		
		numTbl = new TableColumn(table, SWT.NONE);
		numTbl.setWidth(100);
		numTbl.setText("选课人数");
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		composite.setLayout(null);
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(0, 44);
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100);
		composite.setLayoutData(fd_composite);
		
		Label label = new Label(composite, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label.setBounds(10, 6, 71, 22);
		label.setText("查询类型");
		
		combo_type = new Combo(composite, SWT.READ_ONLY);
		combo_type.setItems(new String[] {"全部", "课程编号", "课程名称", "学分", "任课教师"});
		combo_type.setToolTipText("");
		combo_type.setBounds(98, 7, 124, 25);
		combo_type.select(0);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_1.setBounds(244, 6, 32, 22);
		label_1.setText("内容");
		
		text_nrong = new Text(composite, SWT.BORDER);
		text_nrong.setBounds(282, 7, 110, 25);
		
		Button btn_query = new Button(composite, SWT.NONE);
		btn_query.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		
		btn_query.setBounds(398, 5, 77, 27);
		btn_query.setText("查询");
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.left = new FormAttachment(0, 129);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("课程编号");
		
		text_id = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		fd_lblNewLabel.right = new FormAttachment(text_id, -42);
		FormData fd_text_id = new FormData();
		fd_text_id.left = new FormAttachment(0, 240);
		fd_text_id.top = new FormAttachment(lblNewLabel, 1, SWT.TOP);
		text_id.setLayoutData(fd_text_id);
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		fd_text_id.right = new FormAttachment(lblNewLabel_1, -75);
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblNewLabel_1.setText("课程名称");
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(lblNewLabel, 0, SWT.TOP);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		
		text_name = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_text_name = new FormData();
		fd_text_name.right = new FormAttachment(lblNewLabel_1, 144, SWT.RIGHT);
		fd_text_name.left = new FormAttachment(lblNewLabel_1, 21);
		fd_text_name.top = new FormAttachment(lblNewLabel, 1, SWT.TOP);
		text_name.setLayoutData(fd_text_name);
		
		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		fd_lblNewLabel.bottom = new FormAttachment(lblNewLabel_2, -38);
		lblNewLabel_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblNewLabel_2.setText("学分");
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_lblNewLabel_2 = new FormData();
		fd_lblNewLabel_2.left = new FormAttachment(0, 142);
		lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
		
		text_credite = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_text_credite = new FormData();
		fd_text_credite.left = new FormAttachment(lblNewLabel_2, 66);
		fd_text_credite.top = new FormAttachment(lblNewLabel_2, 1, SWT.TOP);
		text_credite.setLayoutData(fd_text_credite);
		
		Label lblNewLabel_1_1 = new Label(this, SWT.NONE);
		fd_lblNewLabel_1.left = new FormAttachment(lblNewLabel_1_1, 0, SWT.LEFT);
		fd_text_credite.right = new FormAttachment(lblNewLabel_1_1, -75);
		fd_lblNewLabel_2.top = new FormAttachment(lblNewLabel_1_1, 0, SWT.TOP);
		lblNewLabel_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		lblNewLabel_1_1.setText("任课教师");
		lblNewLabel_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_lblNewLabel_1_1 = new FormData();
		fd_lblNewLabel_1_1.left = new FormAttachment(0, 438);
		lblNewLabel_1_1.setLayoutData(fd_lblNewLabel_1_1);
		
		text_teacher = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		fd_lblNewLabel_1_1.top = new FormAttachment(0, 541);
		FormData fd_text_teacher = new FormData();
		fd_text_teacher.bottom = new FormAttachment(text_name, 59, SWT.BOTTOM);
		fd_text_teacher.top = new FormAttachment(text_name, 36);
		fd_text_teacher.right = new FormAttachment(lblNewLabel_1_1, 144, SWT.RIGHT);
		fd_text_teacher.left = new FormAttachment(lblNewLabel_1_1, 21);
		text_teacher.setLayoutData(fd_text_teacher);
		
		Button btn_select = new Button(this, SWT.NONE);
		FormData fd_btn_select = new FormData();
		fd_btn_select.right = new FormAttachment(0, 380);
		fd_btn_select.bottom = new FormAttachment(text_credite, 71, SWT.BOTTOM);
		fd_btn_select.top = new FormAttachment(text_credite, 37);
		fd_btn_select.left = new FormAttachment(0, 226);
		btn_select.setLayoutData(fd_btn_select);
		btn_select.setText("选择");
		
		Button button = new Button(this, SWT.NONE);
		fd_table.bottom = new FormAttachment(button, -25);
		
		FormData fd_button = new FormData();
		fd_button.left = new FormAttachment(0, 69);
		button.setLayoutData(fd_button);
		button.setText("上一页");
		
		Button button_1 = new Button(this, SWT.NONE);
		fd_button.top = new FormAttachment(button_1, 0, SWT.TOP);
	
		FormData fd_button_1 = new FormData();
		fd_button_1.bottom = new FormAttachment(text_id, -35);
		button_1.setLayoutData(fd_button_1);
		button_1.setText("下一页");
		
		text_page = new Text(this, SWT.NONE);
		fd_button.right = new FormAttachment(text_page, -41);
		fd_button_1.left = new FormAttachment(0, 212);
		text_page.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		text_page.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_text_page = new FormData();
		fd_text_page.top = new FormAttachment(button_1, 1, SWT.TOP);
		fd_text_page.right = new FormAttachment(button_1, -26);
		fd_text_page.left = new FormAttachment(0, 158);
		text_page.setLayoutData(fd_text_page);
		
		showCourse();
		try {
			int number = dao.findDateNum();
			if(StuUtils.checkNum(number, size)) {
				endPage=number/size;
			}else {
				endPage=number/size+1;
			}
		} catch (BizException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		//功能实现
		//上一页
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(page>1) {
					page--;
				}else {
					return;
				}
				
				showCourse();
			}
		});
		//下一页
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
					if(page<endPage) {
						page++;
					}
				showCourse();
				
			}
		});
		
		//选择课程
		btn_select.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectCourse();	
			}
		});
		
		
		
		
		btn_query.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//TODO 查询
				showCourse();
			//	biz.checkCourseNum();
			}
		});
		
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem tableItem =(TableItem) e.item;
				text_id.setText(tableItem.getText(0));
				text_name.setText(tableItem.getText(1));
				text_credite.setText(tableItem.getText(2));
				text_teacher.setText(tableItem.getText(3));
			}
		});
		
		
	}
	SelectCourseBiz biz = new SelectCourseBiz();
	private Text text_page;
	public void showCourse() {
		
		text_page.setText(page+"");
		String type = combo_type.getText().trim();
		String nrong = text_nrong.getText().trim();
		try {
			
			
			if("全部".equals(type) || type.isEmpty()) {
				date = dao.selectPage(page,size);
			}else {
				biz.bizCheck(nrong);
				
				date = dao.findCourse(type, nrong);
			}
			
			if(StuUtils.isEmpty(date)) {
				StuUtils.showMessage(getShell(), "暂无课程，请等待管理员添加...");
				return;
			}
			table.removeAll();
			//刷新数据
			TableItem tableItem =null;
			//cid,cname,ccreadit,tname,classroom,
			for (Map<String, Object> map : date) {
				courseNum.put(map.get("CID").toString(),numCourse(map.get("CID").toString()));
				tableItem = new TableItem(table,SWT.NONE);
				tableItem.setText(new String[] {
						StuUtils.objToString(map.get("CID")),
						StuUtils.objToString(map.get("CNAME")),
						StuUtils.objToString(map.get("CCREADIT")),
						StuUtils.objToString(map.get("TNAME")),
						StuUtils.objToString(map.get("CLASSROOM")),
						StuUtils.objToString(courseNum.get(map.get("CID").toString())+"/"+StuUtils.num)
				});
			}
		} catch (BizException e) {
			e.printStackTrace();
			StuUtils.showMessage(getShell(), e.getMessage());
			return;
		}
		
	}
	public String numCourse(String cid) {
		
		String numberStudent = null;
		try {
			Map<String,Object> map  = dao.selectNum(cid);
			if(StuUtils.isEmpty(map)) {
				numberStudent="0";
			}else {
				numberStudent=map.get("NUM").toString();
			}
	
		} catch (BizException e) {
			e.printStackTrace();
			StuUtils.showMessage(getShell(), e.getMessage());
		}
		return numberStudent;
	}
	
	public void selectCourse() {
		
		try {
			//String sid=StuUtils.objToString(StuUtils.user.get("SID")); //用户id 整合后获取

			String cid = text_id.getText();
			
			biz.checkCourseNum(Integer.valueOf((courseNum.get(cid).toString())));
			biz.repeat(text_id.getText().trim());
			biz.selectedCourseNumber();
			dao.add("10011001001", cid);
			StuUtils.showMessage(getShell(), "选课成功");
			showCourse();		
		} catch (BizException e) {
			e.printStackTrace();
			StuUtils.showMessage(getShell(), e.getMessage());
			return;
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
